package com.eversec.util.command;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import com.eversec.util.properties.PropertiesUtil;

public class ZipUtil {

	/**
	 * 根据文件内容生成zip文件
	 * @param path
	 * @param info
	 * @return
	 */
	public String zip(String path, String info) {
		String zipFileName = path + "/" + TimeUtil.getTime() + ".zip";
		if (info != null) {
			FileUtil fileUtil = new FileUtil();
			if (!fileUtil.checkDir(path)) {
				fileUtil.createDir(path);
			}

			File zipFile = new File(zipFileName);
			ZipOutputStream out = null;
			try {
				out = new ZipOutputStream(new FileOutputStream(zipFile));
				out.putNextEntry(new ZipEntry(TimeUtil.getTime() + ".xml"));

				out.write(info.getBytes(Constant.CHARACTER_SET));
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (out != null) {
						out.flush();
						out.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return zipFileName;
	}
	
	/**
	 * 将字符串压缩成zip文件并获得字节数据
	 * @param path
	 * @param info
	 * @return
	 */
	public byte[] zipToBytes(String path, String info){
		String zipFileName = zip(path, info);
		byte[] zipBytes = StringUtil.getBytes(zipFileName);
		
		return zipBytes;
	}
	
	/**
	 * 根据zip字节数组转换成zip文件
	 * 
	 * @param path
	 * @param bytes
	 */
	public void zipBytesTozipFile(byte[] bytes){
		if(bytes != null){ 
			String home = PropertiesUtil.getValueByKey("common.properties", "filePath");
			if (!home.endsWith(File.separator)) {
				home = home + File.separator;
	        }
			home = home + "sb_zip" + File.separator;
			FileUtil fileUtil = new FileUtil();
			if (!fileUtil.checkDir(home)) {
				fileUtil.createDir(home);
			}
			
			File file  = new File(home + TimeUtil.getTime() + ".zip"); 
			if(file.exists()){ 
				file.delete(); 
			} 
			FileOutputStream fos;
			try {
				fos = new FileOutputStream(file);
				fos.write(bytes,0,bytes.length); 
				fos.flush(); 
				fos.close(); 
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}

	/**
	 * 根据zip文件的字节数组，解析出zip文件中第一个压缩文件内容的字节数组
	 * 
	 * @param info
	 * @return
	 */
	public byte[] unzip(byte[] info) throws Exception{
		byte[] bytes = null;

		if (info != null) {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(
					info));
			ZipEntry z = null;
			if ((z = in.getNextEntry()) != null) {// zip中只有一个文件
				if (!z.isDirectory()) {
					int len = -1;
					byte data[] = new byte[Constant.READ_SIZE];
					while ((len = in.read(data)) != -1) {
						baos.write(data, 0, len);
					}
				}
			}
			baos.flush();
			bytes = baos.toByteArray();
		}

		return bytes;
	}

	/**
	 * 根据zip文件的字节数组，解析出zip文件中第一个压缩文件内容的字符串
	 * 
	 * @param info
	 * @return
	 */
	public String unzipToString(byte[] info) throws Exception{
		String strInfo = "";

		byte[] bytes = unzip(info);
		if (bytes != null) {
			try {
				strInfo = new String(bytes, Constant.CHARACTER_SET);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		return strInfo;
	}

	public static void main(String[] args) {

		byte[] compressed = StringUtil.getBytes("d:\\test\\1342619698203.zip");

//		String message = new ZipUtil().unzipToString(compressed);
//		
//		new ZipUtil().zipBytesTozipFile("12", compressed);
		System.out.println("message: " + new String(compressed));
	}
}
